<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddPayChannelChildToPay extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::table('pay_stream', function (Blueprint $table) {
            $table->unsignedTinyInteger("payChannelChild")->default(0)->comment("底层支付渠道 1支付宝 2微信")->after('payChannel');
        });

        Schema::table('refund_stream', function (Blueprint $table) {
            $table->unsignedBigInteger("orderId")->default(0)->comment('订单表id')->after('refundNo')->index('idx_orderId');
            $table->unsignedTinyInteger("refundChannelChild")->default(0)->comment("底层退款渠道 1支付宝 2微信")->after('refundChannel');
        });

        if (\App\Kuafu\Pay\Model\PayStreamModel::query()->count()) {
            \App\Kuafu\Pay\Model\PayStreamModel::query()->update(['payChannelChild' => \Illuminate\Support\Facades\DB::raw('payChannel')]);
        }

        if (\App\Kuafu\Pay\Model\RefundStreamModel::query()->count()) {
            \App\Kuafu\Pay\Model\RefundStreamModel::query()->update(['refundChannelChild' => \Illuminate\Support\Facades\DB::raw('refundChannel')]);
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::table('pay_stream', function (Blueprint $table) {
            $table->dropColumn('payChannelChild');
        });

        Schema::table('refund_stream', function (Blueprint $table) {
            $table->dropColumn('orderId');
            $table->dropColumn('refundChannelChild');
        });
    }
}
